﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using BYRBBS.Model;
using BYRBBS.Util;
using System.Diagnostics;
using BYRForum.Binding.SectionChoose;
using BYRForum.Binding.SubFavorite;

namespace BYRForum
{
    public partial class FavoritePage : PhoneApplicationPage
    {
        bool if_loaded;
        public FavoritePage()
        {
            InitializeComponent();
            if_loaded = false;
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
                string level=this.NavigationContext.QueryString["level"];
                App.FavoriteBoardList.Items.Clear();
                BoardListBox.ItemsSource = App.FavoriteBoardList.Items;
                App.SubFavoriteBoardList.Items.Clear();
                SubFavoriteListBox.ItemsSource = App.SubFavoriteBoardList.Items;
                String myRequestUrl = ByrApi.apiUrl + "/favorite/"+level + ByrApi.Encode + ByrApi.appkey;
                RequestState state = new RequestState();
                HttpUtil.HttpGetWithParam(state, myRequestUrl, HttpWebRequset_Favorite_Callback);//发送请求
                progressBarWait.Visibility = Visibility.Visible;

            base.OnNavigatedTo(e);
        }
        private void HttpWebRequset_Favorite_Callback(IAsyncResult asynchronousResult)
        {
            try
            {
                //网络有错误将会在这里抛出异常
                string result = HttpUtil.ReadToEndWithParam(asynchronousResult);
                Debug.WriteLine(result);
                string ErrorInfo;
                //如果没有错误，TopTen就是返回结果，ErrorInfo是空字符串
                //如果有错误，比如密码错误，这里TopTen返回为空结果，ErrorInfo是错误信息
                BYRBBS.Model.Favorite favoriteBoards = ParseUtil.ParseXml<BYRBBS.Model.Favorite>(result, out ErrorInfo);
                this.Dispatcher.BeginInvoke((Action)(() =>
                {
                    App.FavoriteBoardList.LoadData(favoriteBoards.BoardsItem);
                    App.SubFavoriteBoardList.LoadData(favoriteBoards.Sub_favorite);
                    progressBarWait.Visibility = Visibility.Collapsed;
                }));
            }
            catch
            {
                Debug.WriteLine("Error");
                this.Dispatcher.BeginInvoke(() =>
                {
                    progressBarWait.Visibility = Visibility.Collapsed;
                });
                //这里处理异常，可以在界面上抛出messagebox
            }
        }

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            BoardItem data = (sender as ListBox).SelectedItem as BoardItem;
            ListBoxItem selectedItem = (sender as ListBox).ItemContainerGenerator.ContainerFromItem(data) as ListBoxItem;

            int nSelect = (sender as ListBox).SelectedIndex;  //选择的项保存在 nSelect中
            //防止出错。
            if (-1 == nSelect)
            {
                return;
            }
            App.TempBoardItem = data;
            App._BoardChoose = (sender as ListBox).SelectedIndex;
            NavigationService.Navigate(new Uri("/BYRForum;component/ThreadsListPage.xaml?BoardName=" + data.Description, UriKind.Relative));
            (sender as ListBox).SelectedIndex = -1;  //最后设置整个ListBox控件选择为空，否则下次改变时获取的仍然是上次的值
        }

        private void SubFavoriteListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SubFavoriteItem data = (sender as ListBox).SelectedItem as SubFavoriteItem;
            ListBoxItem selectedItem = (sender as ListBox).ItemContainerGenerator.ContainerFromItem(data) as ListBoxItem;

            int nSelect = (sender as ListBox).SelectedIndex;  //选择的项保存在 nSelect中
            //防止出错。
            if (-1 == nSelect)
            {
                return;
            }
            NavigationService.Navigate(new Uri("/BYRForum;component/FavoritePage.xaml?level=" + data.Level, UriKind.Relative));
            (sender as ListBox).SelectedIndex = -1;  //最后设置整个ListBox控件选择为空，否则下次改变时获取的仍然是上次的值
        }

    }
}